/*
 * @Author: 白袂 1741866590@qq.com
 * @Date: 2023-09-17 10:12:51
 * @LastEditors: 白袂 1741866590@qq.com
 * @LastEditTime: 2023-09-19 10:45:51
 * @FilePath: \obj_end_2023_9_17\mock\login.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
let Mock = require("mockjs");
const fs = require("fs");
const path = require("path");
const da = require("./../json/login.json");
const { json } = require("express");
module.exports = {
    login(req) {
        //取到用户数据
        
        const data = da;
        // const jsonData = JSON.parse(data);
        let userVerify = "";
        data.forEach(element => {
            if(element.userName == req.userName && element.identity == req.identity) {
                
                if(element.userPsd == req.userPsd) {
                    userVerify = {
                        code: 200,
                        msg: "sucess"
                    }
                    //验证用户状态
                    if (element.store == 2) {
                        userVerify = {
                            code: 403,
                            msg: "user frezz"
                        }
                    } 
                }  else {
                    userVerify = {
                        code: 401,
                        msg: "psd err"
                    }
                }
                
            }
            
        });
        if (userVerify == "") userVerify = {code: "402", msg: "user is undefind"}
        
        
        return userVerify;
    },
    //注册  在界面注册默认注册普通用户
    sign(req) {
        let userExist = false;
        let codeData = "";
        da.forEach(element => {
            if(element.userName == req.userName) {
                //用户已存在
                userExist = true;
                codeData = {
                    code: "401",
                    msg: "user is exist"
                }
            }
        });
        // console.log("userExist", userExist)
        if (userExist) {
            return codeData;
        }
        //异步了
        fs.readFile(path.join(__dirname, "./../json/login.json"), 'utf8', (err, data) => {
            let arr = JSON.parse(data);
            let newArr = arr;
            //判断是否存在该用户
            // console.log(!userExist)
            if (!userExist) {
                // 注册普通用户
            //    console.log('注册用户')
                const signData = {
                    userName: req.userName,
                    userPsd: req.userPsd,
                    identity: 2,  //普通用户
                    store: 1  //1正常 2冻结

                }
                
                // newArr.push(signData)
                const box = JSON.stringify(arr);
                console.log(box)
                fs.writeFile(path.join(__dirname, "./../json/login.json"), box, 'utf8', (err) => {
                    if(err) {
                        console.log(err)
                        // codeData = {
                        //     code: "402",
                        //     msg: "write error"
                        // }
                    } else {
                        console.log("writeFile sucees")
                        // codeData = {
                        //     code: "200",
                        //     msg: "write sucess"
                        // }
                    }
                })

            }

            
        })
        
       
        // return codeData;
        return codeData = {
            code: "200",
            msg: "write sucess"
        }
       
        



       
            
    }
}
